package cn.achao.csr.sequence.controller;

import cn.achao.csr.base.constant.enums.SysSourceEnum;
import cn.achao.csr.base.exception.BusinessException;
import cn.achao.csr.base.utils.ThrowUtils;
import cn.achao.csr.security.annotation.InnerAuth;
import cn.achao.csr.sequence.core.common.Result;
import cn.achao.csr.sequence.core.common.Status;
import cn.achao.csr.sequence.service.SegmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * @Author: achao
 **/
@Slf4j
@RestController
@RequestMapping("/sequence")
@RequiredArgsConstructor
public class SequenceController {

    private final SegmentService segmentService;

    @InnerAuth
    @RequestMapping(value = "/api/segment/get/{key}")
    public Long getSegmentId(@PathVariable("key") @Validated @NotBlank(message = "序列key不能为空") String key) {
        Result res = segmentService.getId(key);
        ThrowUtils.throwIf(Status.EXCEPTION.equals(res.getStatus()), BusinessException.fail("序列获取失败,key不存在", SysSourceEnum.CSR_SEQUENCE.getSysName()));
        return res.getId();
    }

}
